$(function () {
    $("#myGrid").datagrid({
        url:'/train/query.do',
        fit:true,
        fitColumns:true,
        columns:[
            [
                {field:'dept',title:'申请部门',width:100, formatter:function (value) {
                        console.log(value);
                        return value ?  value.name: '';
                    }},
                {field:'employee',title:'申请人员',width:200,formatter:function (value,row,index) {
                        return value ? value.realname:"";}},
                {field:'name',title:'培训名称',width:100},
                {field:'type',title:'培训类型',width:100,formatter:function (value,row,index) {
                        return value==0 ? '外培':"内培";}},
                {field:'organization',title:'培训机构',width:100},
                {field:'totalNumber',title:'培训费用',width:100},
                {field:'trainAddress',title:'培训场所',width:100},
                {field:'duration',title:'所需时间(天)',width:100},
                {field:'applyDate',title:'申请时间  ',width:100},

            ]
        ],
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        toolbar:'#tb',
    })
})

$(function () {
    $("#myDialog").dialog({
        width:700,
        height:500,
        buttons:'#form_btn',
        closed:true,
        onClose:function () {
            $("#myForm").form('clear')
        }
    })
})

function add() {
    $("#myDialog").dialog('open');
    $("#myDialog").dialog('setTitle','新增培训计划');
    $("#password_tr").show();
}

function edit() {
    var row = $("#myGrid").datagrid('getSelected');
    if(!row){
        $.messager.alert('温馨提示','请选择一条数据','warning');
        return;
    }
    if(row.dept){
        row["dept.id"] = row.dept.id;
    } if(row.employee){
        row["employee.id"] = row.employee.id;
    }
    $("#myForm").form('load',row);

    $("#myDialog").dialog('open');
    $("#myDialog").dialog('setTitle','编辑培训计划');
}

function changeState() {
    var row = $("#myGrid").datagrid('getSelected');
    if(!row){
        $.messager.alert('温馨提示','请选择一条数据','warning');
        return;
    }
    $.messager.confirm("确认对话框","你确定要执行操作吗?",function (r) {
        if(r){
            $.get('/train/changeState.do',{id:row.id},function (data) {
                if(data.success){
                    $.messager.alert("温馨提示","恭喜成功",'info',function () {
                        cancel();
                        reload();
                    })
                }else{
                    $.messager.alert("温馨提示","失败了哦!",'warning')
                }
            })
        }
    })
}

function reload() {
    $("#myGrid").datagrid('reload');
}

function save() {
    $("#myForm").form("submit",{
        url:'/train/saveOrUpdate.do',
        success:function (data) {
            data = $.parseJSON(data,true);
            if(data.success){
                $.messager.alert("温馨提示","保存成功",'info',function () {
                    cancel();
                    reload();
                })
            }else{
                $.messager.alert("温馨提示","保存失败",'warning')
            }
        }
    })
}

function cancel() {
    $("#myDialog").dialog('close');
}


function cearchForm() {
    //获取文本框的内容
    var keyword = $("#keyword").textbox("getValue");
    var deptId = $("#deptId").combobox("getValue");
    var employeeId = $("#employeeId").combobox("getValue");
    var beginDate = $("#beginDate").datebox("getValue");
    var endDate = $("#endDate").datebox("getValue");

    //重新加载数据表格,带上自定义的查询参数
    $("#myGrid").datagrid("load", {
        keyword: keyword,
        deptId:deptId,
        endDate: endDate,
        beginDate: beginDate,
        employeeId:employeeId,
        beginDate:beginDate
    })
}